﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using KingRoad.EPM.InvestmeEntity;
using DotNet.Service;
using DotNet.Model.MyEntity;
using KingRoad.EPM.Web.ViewModels;


namespace KingRoad.EPM.Web.Areas.Permission.Controllers
{
    /// <summary>
    /// 岗位管理
    /// 功能描述：岗位的增删改查、移动（包含岗位树形结构）
    /// 模块负责人：WS
    /// 完成日期：2011.10.21
    /// </summary>
    public class DutyController : Controller
    {
        //
        // GET: /Permission/Duty/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 岗位树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult DutyTree(string id,string name)
        {
            ViewBag.ID = id.Substring(2);
            ViewBag.Name = ServiceManager.Instance.OrganizeService.GetOrganizeEntityById(id.Substring(2)).FullName;
            return View();
        }

        /// <summary>
        /// 获取岗位树
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetDutyNodes(int id=0)
        {
            List<Base_Duty> list = null;
            if (id == 0)
            {
                list = ServiceManager.Instance.OrganizeService.GetDTDuty() as List<Base_Duty>;
            }
            list.Add(new Base_Duty { ID = 0, ParentId = -1, Name = "岗位" });
            return Json(list);
        }

        /// <summary>
        /// 显示某个岗位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShowDutyDetail(string id)
        {
            Base_Duty detail = ServiceManager.Instance.OrganizeService.GetDutyEntityById(id);
            return View(detail);
        }

        /// <summary>
        /// 创建岗位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult CreateDuty(string id)
        {
            ViewBag.ParentID = id;
            if (String.IsNullOrEmpty(id)) ViewBag.ParentID = "0";
            return View();
        }

        /// <summary>
        /// 编辑岗位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditDuty(string id)
        {
            Base_Duty detail = ServiceManager.Instance.OrganizeService.GetDutyEntityById(id);
            return View(detail);
        }

        //拖拽操作
        public ActionResult Move(int id, int pid)
        {
            string statuCode;
            string statuMessage;
            ServiceManager.Instance.OrganizeService.MoveTo(id.ToString(), pid.ToString(), out statuCode, out statuMessage);
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = "closeCurrent"
            });
        }

        /// <summary>
        /// POST 
        /// </summary>
        /// <param name="detail"></param>
        /// <returns></returns>
        public ActionResult DutyCreating(Base_Duty detail)
        {
            string statuCode = string.Empty;
            string statuMessage = string.Empty;
            if (ModelState.IsValid)//实体验证
            {
                //if(model.ParentId)
                detail.DeletionStateCode = 0;
                string id = ServiceManager.Instance.OrganizeService.AddDuty(detail, out statuCode, out statuMessage);
                //返回岗位实体添加树节点
                detail.ID = int.Parse(id);
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode == "OKAdd" ? "200" : "300",
                callbackType = "Create",
                data = detail
            });
        }

        /// <summary>
        /// POST
        /// </summary>
        /// <param name="detail"></param>
        /// <returns></returns>
        public ActionResult DutyEditing(Base_Duty detail)
        {
            string statuCode = string.Empty;
            string statuMessage = string.Empty;
            if (ModelState.IsValid)//实体验证
            {
                detail.DeletionStateCode = 0;
                int id = ServiceManager.Instance.OrganizeService.EditDudy(detail, out statuCode, out statuMessage);
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = "Edit",
                data = detail
            });
        }

        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            string statuCode;
            string statuMessage;
            ServiceManager.Instance.OrganizeService.DeleteDuty(id.ToString(), out statuCode, out statuMessage);
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = "close"
            });
        }
    }
}
